using Magiq.Delete;
using Magiq.Support.Database;

namespace Magiq.Sql {
    public class MagiqToSqlDeleteProvider : MagiqToSqlOperationsProvider, IDeleteStrategy {
        #region IDeleteStrategy Members

        public int Execute<T>(IQueryableDelete<T> delete) where T : class {
            var dataContext = delete.Where.DataContext();
            var dataProvider = new LinqToSqlDatabaseAdapter(dataContext);
            var queryExecutor = new DeleteExecutor(dataProvider, delete.Where);
            var from = queryExecutor.BaseWhereClause;
            var deleteSql = "DELETE " + queryExecutor.BaseTableName + " FROM " + from;

            var result = queryExecutor.ExecuteNonQuery(deleteSql);
            return result;
        }

        #endregion
    }
}